import { h, ref, unref, reactive } from 'vue'
import type { DepartmentItem } from '@/api/department/types'
import { useTable } from '@/hooks/web/useTable'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { routeDelete,routeList,airportList} from '@/api/resource-manage'
import { findDictTypeList} from '@/api/common'
import tabButton from '../components/tabButton.vue'
import { dataToLabel,dataToSelect} from '@/utils'
import { dictType} from '@/utils/dict'
let router:any = null
/**
 *  获取路由对象
 */
export function getRouterInstance(r) {
  router = r
}

let DT:any = null

// 获取data对象
export function getDataInstance(data) {
  DT = data
}

//  挂载类型
export const setMountType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'airport'}) as any

}

export const airportArr = ref<dictType[]>([]) 
 const airportToLabel = ref({})
// 机场 字典数据类型
export const getAirportList = async function () {
  const {data} = await  airportList({}) as any
  airportArr.value  = dataToSelect(data.records, { label: 'name', value: 'id' }) 
 airportToLabel.value = dataToLabel(data.records, 'id','name')
}



// 列表页对象
export default reactive<CrudSchema[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    search: {
      hidden: true
    },
  },
  {
    field: 'name',
    label: '航线名称',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.name
        }
      }
    },
  },
  {
    field: 'length',
    label: '航线长度（m）',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.length
        }
      }
    },
    search:{
      hidden: true
    }
  },
  {
    field: 'airportId',
    label: '关联机场',
    table: {
      slots: {
        default: (data: any) => {
          return airportToLabel.value[data.row.airportId]
        }
      }
    },
  }, 
  {
    field: 'createUserName',
    label: '上传人',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.createUserName
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'action',
    width: '280px',
    label: '操作',
    search: {
      hidden: true 
    },
    table: {
      slots: {
        default: (data: any) => {
          return h(tabButton, {
            actions: [
              {
                type: 'button',
                tip: '详情',
                props: {
                  type: 'success',
                  onClick: () =>  DT.hanleAction(data.row, 'detail')
                }
              },
              {
                type: 'button',
                tip: '编辑',
                props: {
                  type: 'primary',
                  onClick: () => DT.hanleAction(data.row, 'edit')
                }
              },
              {
                type: 'button',
                tip: '删除',
                props: {
                  type: 'danger',
                  onClick: () => DT.delData(data.row)
                }
              }
            ]
          })
        }
      }
    }
  }
])
